home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_long.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-10-18  |  16.2 KB  |  599 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from test.test_support import verify, verbose, TestFailed, fcmp
  5. from string import join
  6. from random import random, randint
  7. SHIFT = 15
  8. BASE = 2 ** SHIFT
  9. MASK = BASE - 1
  10. KARATSUBA_CUTOFF = 70
  11. MAXDIGITS = 15
  12. special = map(long, [
  13.     0,
  14.     1,
  15.     2,
  16.     BASE,
  17.     BASE >> 1])
  18. special.append(0x5555555555555555L)
  19. special.append(0xAAAAAAAAAAAAAAAAL)
  20. p2 = 0x4L
  21. for i in range(2 * SHIFT):
  22.     special.append(p2 - 1)
  23.     p2 = p2 << 1
  24.  
  25. del p2
  26. special = special + map((lambda x: ~x), special) + map((lambda x: -x), special)
  27.  
  28. def check(ok, *args):
  29.     if not ok:
  30.         raise TestFailed, join(map(str, args), ' ')
  31.     
  32.  
  33.  
  34. def getran(ndigits):
  35.     verify(ndigits > 0)
  36.     nbits_hi = ndigits * SHIFT
  37.     nbits_lo = (nbits_hi - SHIFT) + 1
  38.     answer = 0x0L
  39.     nbits = 0
  40.     r = int(random() * SHIFT * 2) | 1
  41.     while nbits < nbits_lo:
  42.         bits = (r >> 1) + 1
  43.         bits = min(bits, nbits_hi - nbits)
  44.         None(verify if bits <= bits else bits <= SHIFT)
  45.         nbits = nbits + bits
  46.         answer = answer << bits
  47.         if r & 1:
  48.             answer = answer | (1 << bits) - 1
  49.         
  50.         r = int(random() * SHIFT * 2)
  51.     None(verify if nbits <= nbits else nbits <= nbits_hi)
  52.     if random() < 0.5:
  53.         answer = -answer
  54.     
  55.     return answer
  56.  
  57.  
  58. def getran2(ndigits):
  59.     answer = 0x0L
  60.     for i in range(ndigits):
  61.         answer = answer << SHIFT | randint(0, MASK)
  62.     
  63.     if random() < 0.5:
  64.         answer = -answer
  65.     
  66.     return answer
  67.  
  68.  
  69. def test_division_2(x, y):
  70.     (q, r) = divmod(x, y)
  71.     q2 = x // y
  72.     r2 = x % y
  73.     pab = x * y
  74.     pba = y * x
  75.     check(pab == pba, 'multiplication does not commute for', x, y)
  76.     check(q == q2, 'divmod returns different quotient than / for', x, y)
  77.     check(r == r2, 'divmod returns different mod than % for', x, y)
  78.     check(x == q * y + r, 'x != q*y + r after divmod on', x, y)
  79.     if y > 0:
  80.         None(check if r <= r else r < y, 'bad mod from divmod on', x, y)
  81.     elif r < r:
  82.         pass
  83.     
  84.     check(r <= 0, 'bad mod from divmod on', x, y)
  85.  
  86.  
  87. def test_division(maxdigits = MAXDIGITS):
  88.     if verbose:
  89.         print 'long / * % divmod'
  90.     
  91.     digits = range(1, maxdigits + 1) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 14)
  92.     digits.append(KARATSUBA_CUTOFF * 3)
  93.     for lenx in digits:
  94.         x = getran(lenx)
  95.         for leny in digits:
  96.             if not getran(leny):
  97.                 pass
  98.             y = 0x1L
  99.             test_division_2(x, y)
  100.         
  101.     
  102.  
  103.  
  104. def test_karatsuba():
  105.     if verbose:
  106.         print 'Karatsuba'
  107.     
  108.     digits = range(1, 5) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 10)
  109.     digits.extend([
  110.         KARATSUBA_CUTOFF * 10,
  111.         KARATSUBA_CUTOFF * 100])
  112.     bits = [ digit * SHIFT for digit in digits ]
  113.     for abits in bits:
  114.         a = (0x1L << abits) - 1
  115.         for bbits in bits:
  116.             b = (0x1L << bbits) - 1
  117.             x = a * b
  118.             y = ((0x1L << abits + bbits) - (0x1L << abits) - (0x1L << bbits)) + 1
  119.             check(x == y, 'bad result for', a, '*', b, x, y)
  120.         
  121.     
  122.  
  123.  
  124. def test_bitop_identities_1(x):
  125.     check(x & 0 == 0, 'x & 0 != 0 for', x)
  126.     check(x | 0 == x, 'x | 0 != x for', x)
  127.     check(x ^ 0 == x, 'x ^ 0 != x for', x)
  128.     check(x & -1 == x, 'x & -1 != x for', x)
  129.     check(x | -1 == -1, 'x | -1 != -1 for', x)
  130.     check(x ^ -1 == ~x, 'x ^ -1 != ~x for', x)
  131.     check(x == ~~x, 'x != ~~x for', x)
  132.     check(x & x == x, 'x & x != x for', x)
  133.     check(x | x == x, 'x | x != x for', x)
  134.     check(x ^ x == 0, 'x ^ x != 0 for', x)
  135.     check(x & ~x == 0, 'x & ~x != 0 for', x)
  136.     check(x | ~x == -1, 'x | ~x != -1 for', x)
  137.     check(x ^ ~x == -1, 'x ^ ~x != -1 for', x)
  138.     None(check if 1 + ~x == 1 + ~x else 1 + ~x == ~(x - 1), 'not -x == 1 + ~x == ~(x-1) for', x)
  139.     for n in range(2 * SHIFT):
  140.         p2 = 0x2L ** n
  141.         check(x << n >> n == x, 'x << n >> n != x for', x, n)
  142.         check(x // p2 == x >> n, 'x // p2 != x >> n for x n p2', x, n, p2)
  143.         check(x * p2 == x << n, 'x * p2 != x << n for x n p2', x, n, p2)
  144.         None(check if (x >> n) << n == (x >> n) << n else (x >> n) << n == x & ~(p2 - 1), 'not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2', x, n, p2)
  145.     
  146.  
  147.  
  148. def test_bitop_identities_2(x, y):
  149.     check(x & y == y & x, 'x & y != y & x for', x, y)
  150.     check(x | y == y | x, 'x | y != y | x for', x, y)
  151.     check(x ^ y == y ^ x, 'x ^ y != y ^ x for', x, y)
  152.     check(x ^ y ^ x == y, 'x ^ y ^ x != y for', x, y)
  153.     check(x & y == ~(~x | ~y), 'x & y != ~(~x | ~y) for', x, y)
  154.     check(x | y == ~(~x & ~y), 'x | y != ~(~x & ~y) for', x, y)
  155.     check(x ^ y == (x | y) & ~(x & y), 'x ^ y != (x | y) & ~(x & y) for', x, y)
  156.     check(x ^ y == x & ~y | ~x & y, 'x ^ y == (x & ~y) | (~x & y) for', x, y)
  157.     check(x ^ y == (x | y) & (~x | ~y), 'x ^ y == (x | y) & (~x | ~y) for', x, y)
  158.  
  159.  
  160. def test_bitop_identities_3(x, y, z):
  161.     check(x & y & z == x & y & z, '(x & y) & z != x & (y & z) for', x, y, z)
  162.     check(x | y | z == x | y | z, '(x | y) | z != x | (y | z) for', x, y, z)
  163.     check(x ^ y ^ z == x ^ y ^ z, '(x ^ y) ^ z != x ^ (y ^ z) for', x, y, z)
  164.     check(x & (y | z) == x & y | x & z, 'x & (y | z) != (x & y) | (x & z) for', x, y, z)
  165.     check(x | y & z == (x | y) & (x | z), 'x | (y & z) != (x | y) & (x | z) for', x, y, z)
  166.  
  167.  
  168. def test_bitop_identities(maxdigits = MAXDIGITS):
  169.     if verbose:
  170.         print 'long bit-operation identities'
  171.     
  172.     for x in special:
  173.         test_bitop_identities_1(x)
  174.     
  175.     digits = range(1, maxdigits + 1)
  176.     for lenx in digits:
  177.         x = getran(lenx)
  178.         test_bitop_identities_1(x)
  179.         for leny in digits:
  180.             y = getran(leny)
  181.             test_bitop_identities_2(x, y)
  182.             test_bitop_identities_3(x, y, getran((lenx + leny) // 2))
  183.         
  184.     
  185.  
  186.  
  187. def slow_format(x, base):
  188.     if (x, base) == (0, 8):
  189.         return '0L'
  190.     
  191.     digits = []
  192.     sign = 0
  193.     if x < 0:
  194.         sign = 1
  195.         x = -x
  196.     
  197.     while x:
  198.         (x, r) = divmod(x, base)
  199.         digits.append(int(r))
  200.     digits.reverse()
  201.     if not digits:
  202.         pass
  203.     digits = [
  204.         0]
  205.     return '-'[:sign] + {
  206.         8: '0',
  207.         10: '',
  208.         16: '0x' }[base] + join(map((lambda i: '0123456789ABCDEF'[i]), digits), '') + 'L'
  209.  
  210.  
  211. def test_format_1(x):
  212.     atol = atol
  213.     import string
  214.     for base, mapper in ((8, oct), (10, repr), (16, hex)):
  215.         got = mapper(x)
  216.         expected = slow_format(x, base)
  217.         check(got == expected, mapper.__name__, 'returned', got, 'but expected', expected, 'for', x)
  218.         check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
  219.     
  220.     got = str(x)
  221.     expected = slow_format(x, 10)[:-1]
  222.     check(got == expected, mapper.__name__, 'returned', got, 'but expected', expected, 'for', x)
  223.  
  224.  
  225. def test_format(maxdigits = MAXDIGITS):
  226.     if verbose:
  227.         print 'long str/hex/oct/atol'
  228.     
  229.     for x in special:
  230.         test_format_1(x)
  231.     
  232.     for i in range(10):
  233.         for lenx in range(1, maxdigits + 1):
  234.             x = getran(lenx)
  235.             test_format_1(x)
  236.         
  237.     
  238.  
  239.  
  240. def test_misc(maxdigits = MAXDIGITS):
  241.     if verbose:
  242.         print 'long miscellaneous operations'
  243.     
  244.     import sys
  245.     hugepos = sys.maxint
  246.     hugeneg = -hugepos - 1
  247.     hugepos_aslong = long(hugepos)
  248.     hugeneg_aslong = long(hugeneg)
  249.     check(hugepos == hugepos_aslong, 'long(sys.maxint) != sys.maxint')
  250.     check(hugeneg == hugeneg_aslong, 'long(-sys.maxint-1) != -sys.maxint-1')
  251.     
  252.     try:
  253.         check(int(hugepos_aslong) == hugepos, 'converting sys.maxint to long and back to int fails')
  254.     except OverflowError:
  255.         raise TestFailed, 'int(long(sys.maxint)) overflowed!'
  256.  
  257.     
  258.     try:
  259.         check(int(hugeneg_aslong) == hugeneg, 'converting -sys.maxint-1 to long and back to int fails')
  260.     except OverflowError:
  261.         raise TestFailed, 'int(long(-sys.maxint-1)) overflowed!'
  262.  
  263.     x = hugepos_aslong + 1
  264.     
  265.     try:
  266.         y = int(x)
  267.     except OverflowError:
  268.         raise TestFailed, "int(long(sys.maxint) + 1) mustn't overflow"
  269.  
  270.     if not isinstance(y, long):
  271.         raise TestFailed('int(long(sys.maxint) + 1) should have returned long')
  272.     
  273.     x = hugeneg_aslong - 1
  274.     
  275.     try:
  276.         y = int(x)
  277.     except OverflowError:
  278.         raise TestFailed, "int(long(-sys.maxint-1) - 1) mustn't overflow"
  279.  
  280.     if not isinstance(y, long):
  281.         raise TestFailed('int(long(-sys.maxint-1) - 1) should have returned long')
  282.     
  283.     
  284.     class long2(long):
  285.         pass
  286.  
  287.     x = long2(0x1L << 100)
  288.     y = int(x)
  289.     if type(y) is not long:
  290.         raise TestFailed('overflowing int conversion must return long not long subtype')
  291.     
  292.  
  293.  
  294. def test_auto_overflow():
  295.     import math
  296.     import sys
  297.     if verbose:
  298.         print 'auto-convert int->long on overflow'
  299.     
  300.     special = [
  301.         0,
  302.         1,
  303.         2,
  304.         3,
  305.         sys.maxint - 1,
  306.         sys.maxint,
  307.         sys.maxint + 1]
  308.     sqrt = int(math.sqrt(sys.maxint))
  309.     special.extend([
  310.         sqrt - 1,
  311.         sqrt,
  312.         sqrt + 1])
  313.     []([ -i for i in special ])
  314.     
  315.     def checkit(*args):
  316.         verify(got == expected, 'for %r expected %r got %r' % (args, expected, got))
  317.  
  318.     for x in special:
  319.         longx = long(x)
  320.         expected = -longx
  321.         got = -x
  322.         checkit('-', x)
  323.         for y in special:
  324.             longy = long(y)
  325.             expected = longx + longy
  326.             got = x + y
  327.             checkit(x, '+', y)
  328.             expected = longx - longy
  329.             got = x - y
  330.             checkit(x, '-', y)
  331.             expected = longx * longy
  332.             got = x * y
  333.             checkit(x, '*', y)
  334.             if abs(y) < 5:
  335.                 if x == 0:
  336.                     pass
  337.                 if not (y < 0):
  338.                     expected = longx ** longy
  339.                     got = x ** y
  340.                     checkit(x, '**', y)
  341.                     for z in special:
  342.                         if z != 0:
  343.                             if y >= 0:
  344.                                 expected = pow(longx, longy, long(z))
  345.                                 got = pow(x, y, z)
  346.                                 checkit('pow', x, y, '%', z)
  347.                             else:
  348.                                 
  349.                                 try:
  350.                                     pow(longx, longy, long(z))
  351.                                 except TypeError:
  352.                                     None if y else special.extend
  353.                                     None if y else special.extend
  354.                                 except:
  355.                                     None if y else special.extend
  356.  
  357.                                 raise TestFailed('pow%r should have raised TypeError' % ((longx, longy, long(z)),))
  358.                         y >= 0
  359.                     
  360.             None if y else special.extend
  361.         
  362.     
  363.  
  364.  
  365. def test_float_overflow():
  366.     import math
  367.     if verbose:
  368.         print 'long->float overflow'
  369.     
  370.     for x in (-2.0, -1.0, 0.0, 1.0, 2.0):
  371.         verify(float(long(x)) == x)
  372.     
  373.     shuge = '12345' * 120
  374.     huge = 0x1L << 30000
  375.     mhuge = -huge
  376.     namespace = {
  377.         'huge': huge,
  378.         'mhuge': mhuge,
  379.         'shuge': shuge,
  380.         'math': math }
  381.     for test in [
  382.         'float(huge)',
  383.         'float(mhuge)',
  384.         'complex(huge)',
  385.         'complex(mhuge)',
  386.         'complex(huge, 1)',
  387.         'complex(mhuge, 1)',
  388.         'complex(1, huge)',
  389.         'complex(1, mhuge)',
  390.         '1. + huge',
  391.         'huge + 1.',
  392.         '1. + mhuge',
  393.         'mhuge + 1.',
  394.         '1. - huge',
  395.         'huge - 1.',
  396.         '1. - mhuge',
  397.         'mhuge - 1.',
  398.         '1. * huge',
  399.         'huge * 1.',
  400.         '1. * mhuge',
  401.         'mhuge * 1.',
  402.         '1. // huge',
  403.         'huge // 1.',
  404.         '1. // mhuge',
  405.         'mhuge // 1.',
  406.         '1. / huge',
  407.         'huge / 1.',
  408.         '1. / mhuge',
  409.         'mhuge / 1.',
  410.         '1. ** huge',
  411.         'huge ** 1.',
  412.         '1. ** mhuge',
  413.         'mhuge ** 1.',
  414.         'math.sin(huge)',
  415.         'math.sin(mhuge)',
  416.         'math.sqrt(huge)',
  417.         'math.sqrt(mhuge)',
  418.         'math.floor(huge)',
  419.         'math.floor(mhuge)']:
  420.         
  421.         try:
  422.             eval(test, namespace)
  423.         except OverflowError:
  424.             pass
  425.  
  426.         raise TestFailed('expected OverflowError from %s' % test)
  427.         if float(shuge) == int(shuge):
  428.             raise TestFailed('float(shuge) should not equal int(shuge)')
  429.             continue
  430.     
  431.  
  432.  
  433. def test_logs():
  434.     import math
  435.     if verbose:
  436.         print 'log and log10'
  437.     
  438.     LOG10E = math.log10(math.e)
  439.     for exp in range(10) + [
  440.         100,
  441.         1000,
  442.         10000]:
  443.         value = 10 ** exp
  444.         log10 = math.log10(value)
  445.         verify(fcmp(log10, exp) == 0)
  446.         expected = exp / LOG10E
  447.         log = math.log(value)
  448.         verify(fcmp(log, expected) == 0)
  449.     
  450.     for bad in (-(0x1L << 10000), -0x2L, 0x0L):
  451.         
  452.         try:
  453.             math.log(bad)
  454.             raise TestFailed('expected ValueError from log(<= 0)')
  455.         except ValueError:
  456.             pass
  457.  
  458.         
  459.         try:
  460.             math.log10(bad)
  461.             raise TestFailed('expected ValueError from log10(<= 0)')
  462.         continue
  463.         except ValueError:
  464.             continue
  465.         
  466.  
  467.     
  468.  
  469.  
  470. def test_mixed_compares():
  471.     import math
  472.     import sys
  473.     if verbose:
  474.         print 'mixed comparisons'
  475.     
  476.     
  477.     class Rat:
  478.         
  479.         def __init__(self, value):
  480.             if isinstance(value, (int, long)):
  481.                 self.n = value
  482.                 self.d = 1
  483.             elif isinstance(value, float):
  484.                 (f, e) = math.frexp(abs(value))
  485.                 if not f == 0:
  486.                     if f <= f:
  487.                         pass
  488.                     elif not f < 1.0:
  489.                         raise AssertionError
  490.                 CHUNK = 28
  491.                 top = 0
  492.                 while f:
  493.                     f = math.ldexp(f, CHUNK)
  494.                     digit = int(f)
  495.                     if not digit >> CHUNK == 0:
  496.                         raise AssertionError
  497.                     top = top << CHUNK | digit
  498.                     f -= digit
  499.                     if f <= f:
  500.                         pass
  501.                     elif not f < 1.0:
  502.                         raise AssertionError
  503.                     e -= CHUNK
  504.                 if e >= 0:
  505.                     n = top << e
  506.                     d = 1
  507.                 else:
  508.                     n = top
  509.                     d = 1 << -e
  510.                 if value < 0:
  511.                     n = -n
  512.                 
  513.                 self.n = n
  514.                 self.d = d
  515.                 if not float(n) / float(d) == value:
  516.                     raise AssertionError
  517.             else:
  518.                 raise TypeError("can't deal with %r" % val)
  519.  
  520.         
  521.         def __cmp__(self, other):
  522.             if not isinstance(other, Rat):
  523.                 other = Rat(other)
  524.             
  525.             return cmp(self.n * other.d, self.d * other.n)
  526.  
  527.  
  528.     cases = [
  529.         0,
  530.         0.001,
  531.         0.98999999999999999,
  532.         1.0,
  533.         1.5,
  534.         1e+20,
  535.         9.9999999999999997e+199]
  536.     for t in (2.0 ** 48, 2.0 ** 50, 2.0 ** 53):
  537.         cases.extend([
  538.             t - 1.0,
  539.             t - 0.29999999999999999,
  540.             t,
  541.             t + 0.29999999999999999,
  542.             t + 1.0,
  543.             long(t - 1),
  544.             long(t),
  545.             long(t + 1)])
  546.     
  547.     cases.extend([
  548.         0,
  549.         1,
  550.         2,
  551.         sys.maxint,
  552.         float(sys.maxint)])
  553.     t = long(9.9999999999999997e+199)
  554.     cases.extend([
  555.         0x0L,
  556.         0x1L,
  557.         0x2L,
  558.         0x1L << 20000,
  559.         t - 1,
  560.         t,
  561.         t + 1])
  562.     []([ -x for x in cases ])
  563.     for x in cases:
  564.         Rx = Rat(x)
  565.         for y in cases:
  566.             Ry = Rat(y)
  567.             Rcmp = cmp(Rx, Ry)
  568.             xycmp = cmp(x, y)
  569.             if x == y != Rcmp == 0:
  570.                 raise TestFailed('%r == %r %d' % (x, y, Rcmp))
  571.             
  572.             if x != y != Rcmp != 0:
  573.                 raise TestFailed('%r != %r %d' % (x, y, Rcmp))
  574.             
  575.             if x < y != Rcmp < 0:
  576.                 raise TestFailed('%r < %r %d' % (x, y, Rcmp))
  577.             
  578.             if x <= y != Rcmp <= 0:
  579.                 raise TestFailed('%r <= %r %d' % (x, y, Rcmp))
  580.             
  581.             if (x > y) != (Rcmp > 0):
  582.                 raise TestFailed('%r > %r %d' % (x, y, Rcmp))
  583.             
  584.             if (x >= y) != (Rcmp >= 0):
  585.                 raise TestFailed('%r >= %r %d' % (x, y, Rcmp))
  586.                 continue
  587.         
  588.     
  589.  
  590. test_division()
  591. test_karatsuba()
  592. test_bitop_identities()
  593. test_format()
  594. test_misc()
  595. test_auto_overflow()
  596. test_float_overflow()
  597. test_logs()
  598. test_mixed_compares()
  599.